package kr.pe.osdev.dao.jdbc.mysql;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import kr.pe.osdev.dao.ItemDao;
import kr.pe.osdev.dao.jdbc.BaseJdbcDao;
import kr.pe.osdev.domain.Item;
import kr.pe.osdev.domain.Question;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

@Repository
public class ItemDaoImpl extends BaseJdbcDao implements ItemDao {

    @Override
    public Item add(Item item) throws DataAccessException {
            String sql = getMessageSourceAccessor().getMessage("item.add");
            
            Object[] obj = new Object[] { item.getSid(),
                    item.getContent(),
                    item.getSeq(),
                    item.getAnswerYn(),
                    item.getDisplayCode(),
                    item.getQuestionSid(),
                    item.getUseYn()};
      
            getJdbcTemplate().update(sql, obj);
            
        return item;
    }
    
    @Override
    public boolean addList(List<Item> itemList) throws DataAccessException {
        String sql = getMessageSourceAccessor().getMessage("item.add");
        
        for(Item item : itemList) {
            Object[] obj = new Object[] { item.getSid(),
                    item.getContent(),
                    item.getSeq(),
                    item.getAnswerYn(),
                    item.getDisplayCode(),
                    item.getQuestionSid(),
                    item.getUseYn()};
            
            getJdbcTemplate().update(sql, obj);
        }
        
        return true;
    }

    @Override
    public int count() throws DataAccessException {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public Item find(Item item) throws DataAccessException {
        // TODO Auto-generated method stub
        return null;
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public List<Item> findList() throws DataAccessException {
        String sql = getMessageSourceAccessor().getMessage("item.findList");
        
        return (List<Item>) getJdbcTemplate().query(sql, getRowMapper());
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public List<Item> findListByQuestion(Question question) throws DataAccessException {
        String sql = getMessageSourceAccessor().getMessage("item.findListByQuestionSid");
        
        
        return (List<Item>) getJdbcTemplate().query(sql, new Object[] { question.getSid() }, getRowMapper());
    }

    @Override
    public Item modify(Item item) throws DataAccessException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Item remove(Item item) throws DataAccessException {
        // TODO Auto-generated method stub
        return null;
    }
    
    @Override
    public int removeByQuestion(Question question) throws DataAccessException {
        String sql = getMessageSourceAccessor().getMessage("item.removeByQuestion");

        return getJdbcTemplate().update(sql, new Object[] { question.getSid() });        
    }
    
    private RowMapper getRowMapper() {
        RowMapper rowMapper = new RowMapper() {
            public Object mapRow(ResultSet rs, int rownum) throws SQLException {
                Item item = new Item();
                item.setSid(rs.getString("SID"));
                item.setContent(rs.getString("CONTENT"));
                item.setSeq(rs.getInt("SEQ"));
                item.setAnswerYn(rs.getString("ANSWER_YN"));
                item.setDisplayCode(rs.getString("DISPLAY_CODE"));
                item.setQuestionSid(rs.getString("QUESTION_SID"));

                return item;
            }
        };
        
        return rowMapper;
    }    
}
